package networking;

/*
Compile client and server and then start server as follows:

$ java GreetingServer 6066
Waiting for client on port 6066...
Check client program as follows:

$ java GreetingClient localhost 6066
Connecting to localhost on port 6066
Just connected to localhost/127.0.0.1:6066
Server says Thank you for connecting to /127.0.0.1:6066
Goodbye!
*/
import java.net.*;
import java.io.*;

public class GreetingClient
{
	   public static void main(String [] args)
	   {
	      String serverName = "localhost";
	      int port = Integer.parseInt("6066");
	      try
	      {
	         System.out.println("Client: Connecting to " + serverName + " on port " + port);
	         Socket client = new Socket(serverName, port);
	         System.out.println("Client: Just connected to " + client.getRemoteSocketAddress());
	         OutputStream outToServer = client.getOutputStream();
	         DataOutputStream out = new DataOutputStream(outToServer);
	         
	         out.writeUTF("Hello from client at" + client.getLocalSocketAddress());
	         InputStream inFromServer = client.getInputStream();
	         DataInputStream in = new DataInputStream(inFromServer);
	         System.out.println("Client gets the message from the server: "+in.readUTF());
	         client.close();
	      }catch(IOException e)
	      {
	         e.printStackTrace();
	      }
	   }
}